package com.hxzy.controller.mobile.cart;

import com.hxzy.common.annotates.StpMobileCheckLogin;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.resp.R;
import com.hxzy.common.util.CartFactory;
import com.hxzy.controller.mobile.cart.req.CartAddReq;
import com.hxzy.controller.mobile.cart.req.MobileAddCartNumReq;
import com.hxzy.controller.mobile.cart.resp.MobileCartResp;
import com.hxzy.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author wy
 * @Description TODO
 * @Date 2024/1/17 10:59
 * @Version 1.0
 */
@RestController
@RequestMapping(value = "/mobile/cart")
@Api(tags = "手机-购物车API")
@StpMobileCheckLogin
public class MobileCartController {

    @Autowired
    private ShoppingCartService  shoppingCartService;

    @Autowired
    private CartFactory cartFactory;

    @ApiOperation(value = "用户某个商家的购物车信息")
    @GetMapping(value = "/merchantId/{merchantId}")
    public R<List<MobileCartResp>>  cartList(@PathVariable(value = "merchantId") Long merchantId){
        List<MobileCartResp> list= this.shoppingCartService.cartList(merchantId);
        return R.okHasData(list);
    }

    @ApiOperation(value = "添加到购物车")
    @PostMapping(value = "/add")
    public R cartAdd(@RequestBody @Valid CartAddReq req){

        //number
        //int number=this.shoppingCartService.cartAdd(req);
        int number=this.cartFactory.getCartStorategy().cartAdd(req);

        Map<String,Integer> dataMap=new HashMap<>();
        dataMap.put("number",number);
        return R.okHasData(dataMap) ;
    }

    @ApiOperation(value = "增加购物车商品数量")
    @PostMapping(value = "/addnum")
    public R incrementNum(@RequestBody @Valid MobileAddCartNumReq req){
       // int number=this.shoppingCartService.incrementNum(req);
        int number=this.cartFactory.getCartStorategy().incrementNum(req);
        Map<String,Integer> dataMap=new HashMap<>();
        dataMap.put("number",number);
        return R.okHasData(dataMap) ;
    }

    @ApiOperation(value = "减少购物车商品数量")
    @PostMapping(value = "/sub")
    public R subNum(@RequestBody @Valid MobileAddCartNumReq req){
      //  int number=this.shoppingCartService.subNum(req);
        int number=this.cartFactory.getCartStorategy().subNum(req);
        Map<String,Integer> dataMap=new HashMap<>();
        dataMap.put("number",number);
        return R.okHasData(dataMap) ;
    }

    @ApiOperation(value = "清空当前用户某个商家的购物车所有数据")
    @DeleteMapping(value = "/clean/{merchantId}")
    public R clearCart(@PathVariable(value = "merchantId") Long merchantId){
        //boolean result=this.shoppingCartService.cleanCart(merchantId);
        boolean result=this.cartFactory.getCartStorategy().cleanCart(merchantId);
        return result?R.ok(): R.build(AckCode.DATABASE_ERROR);
    }


    @ApiOperation(value = "清除当前用户某个商家中某一个商品的信息")
    @DeleteMapping(value = "/cleans")
    public R clearMerchantIdAndProductId(@RequestBody @Valid MobileAddCartNumReq req ){
       // this.shoppingCartService.clearMerchantIdAndProductId(req);
        this.cartFactory.getCartStorategy().clearMerchantIdAndProductId(req);
        return R.ok();
    }


}
